<?php
/**
 *  道具购买
 */

class BuyProp extends APIBase {
	public $tag = "BuyProp";
	public $isLogin = true;
	public function before() {
		$this->initMysql();
		$this->inithttp();
		$this->initCacheRedis();
		return true;
	}
			
	public function logic() {
	   
		if (!isset($this->param['propid'])) {
			$this->returnError(310, "propid must be needed");
		}

	    $playerID = $this->uid;
		//判断是否为vip
		$is_vip = $this->data_redis->hGet("hu:".$playerID,"is_vip");
		$where['id'] = (int)$this->param['propid'];//商品id
		$goods = $this->mysql->select('goods','*',$where);
		if(!count($goods)) {     
			$this->returnError(310, "propid is not exists");
		}
		//判断钻石是否够买
		//$user = $this->data_redis->hMget("hu:{$this->uid}", array('coin','room_card','room_card_coop','money'));
		$user = $this->data_redis->hMget("hu:{$this->uid}", array('coin','room_card','money'));
        $buy_money = (int)$goods[0]['buy_money'];
		$num = (int)$goods[0]['money'];
		
		if((int)$buy_money >$user['coin'] ){
		  	$this->returnError(319, "钻石不足不能购买商品!"); 
		}
		
		$symoney = (int)$user['coin']-(int)$buy_money;
		$this->data_redis->hMset("hu:{$playerID}",array('coin'=>$symoney));
		
		//获取背包数据
		$backpacklist = $this->cache_redis->keys("hbackpack:$playerID:*");
		
		//道具详情
		$propid = (int)$goods[0]['propid'];	
		$prop = $this->cache_redis->hGetAll("prop:".$propid);
		
		//商品类型
		$type = (int)$goods[0]['type'];
		
		if($type==2){//独立型房卡room_card
			$num = (int)$user['room_card']+$num;
			$time = time();
			/*
			$propidArr = array();
			for($i=0;$i<count($backpacklist);$i++){
				$backpack = $this->cache_redis->hGetAll("hbackpack:$playerID:".$i);
				if((int)$backpack['propid']==$propid){
					$this->cache_redis->hMset("hbackpack:$playerID:".$i,array('count'=>$num,'createtime'=>time()));
				}
				$propidArr[] = (int)$backpack['propid'];
			}
			if(!in_array($propid,$propidArr)){
				    $this->cache_redis->hMset("hbackpack:$playerID:".count($backpacklist),array('uid'=>$playerID,'count'=>$num,'propid'=>$propid,'type'=>$prop['type'],'createtime'=>time()));
			}
			*/
    	    $this->data_redis->hMset("hu:{$playerID}", array('room_card'=>$num));
			$reqData = array('coin'=>$symoney,'room_card'=>$num,'money'=>$user['money']);
			//$reqData = array('coin'=>$symoney,'room_card'=>$num,'room_card_coop'=>$user['room_card_coop']);
			
		}elseif($type==3){//共付型房卡room_card_coop
		/*
		    $symoney = (int)$user['coin']-(int)$buy_money;
		    $this->data_redis->hMset("hu:{$this->uid}",array('coin'=>$symoney));
			$num = (int)$user['room_card_coop']+$num;
			$time = time();
			$propidArr = array();
			for($i=0;$i<count($backpacklist);$i++){
				$backpack = $this->cache_redis->hGetAll("hbackpack:$playerID:".$i);
				if((int)$backpack['propid']==$propid){
					$this->cache_redis->hMset("hbackpack:$playerID:".$i,array('count'=>$num,'createtime'=>time()));
				}
				$propidArr[] = (int)$backpack['propid'];
			}
			if(!in_array($propid,$propidArr)){
				    $this->cache_redis->hMset("hbackpack:$playerID:".count($backpacklist),array('uid'=>$playerID,'count'=>$num,'propid'=>$propid,'type'=>$prop['type'],'createtime'=>time()));
			}
			$this->data_redis->hMset("hu:{$this->uid}", array('room_card_coop'=>$num));
            $reqData = array('coin'=>$symoney,'room_card_coop'=>$num,'room_card'=>$user['room_card']);	
		*/	
	    }elseif($type==4 || $type==5 || $type==6){//表情
		/*
		    for($i=0;$i<count($backpacklist);$i++){
				$backpack = $this->cache_redis->hGetAll("hbackpack:$playerID:".$i);
				$propidArr[] = (int)$backpack['propid'];
			}
			
			if(!in_array($propid,$propidArr)){
				 $symoney = (int)$user['coin']-(int)$buy_money;
		         $this->data_redis->hMset("hu:{$this->uid}",array('coin'=>$symoney));
				 $this->cache_redis->hMset("hbackpack:$playerID:".count($backpacklist),array('uid'=>$playerID,'count'=>7,'propid'=>$propid,'type'=>$prop['type'],'createtime'=>time()));
				 $reqData = array('coin'=>$symoney,'room_card_coop'=>$user['room_card_coop'],'room_card'=>$user['room_card']);
			}else{
				 $this->returnError(301,'已购买!');
				 exit;
			}
		*/
		}elseif($type==8){//购买金币
		    if($is_vip){
				$viprow = $this->mysql->select("vipdays","level",array("uid"=>$playerID));
				$vipconfig = $this->mysql->select("vip_config","benefit",array("level"=>$viprow[0]['level']));
				$benefit = ((int)$goods[0]['money']*(int)$vipconfig[0]['benefit']) / 100;
				$num = $num+$benefit;
			}
	        
			$num = (int)$user['money']+$num;
			$time = time();
			/*
			$propidArr = array();
			for($i=0;$i<count($backpacklist);$i++){
				$backpack = $this->cache_redis->hGetAll("hbackpack:$playerID:".$i);
				if((int)$backpack['propid']==$propid){
					$this->cache_redis->hMset("hbackpack:$playerID:".$i,array('count'=>$num,'createtime'=>time()));
				}
				$propidArr[] = (int)$backpack['propid'];
			}
			if(!in_array($propid,$propidArr)){
				    $this->cache_redis->hMset("hbackpack:$playerID:".count($backpacklist),array('uid'=>$playerID,'count'=>$num,'propid'=>$propid,'type'=>$prop['type'],'createtime'=>time()));
			}
			*/
    	    $this->data_redis->hMset("hu:{$playerID}", array('money'=>$num));
			$logdata =  array(
				            'type'=>8,
				            'vid' => 0,
				            'tid'=>  0,
				            'seatid'=> 0,
				            'uid' => $playerID,
				            'money' => (int)$user['money']+$num,
							'change_money'=>$num,
				            'create_time' => time(),
			            );
            $this->addChangeMongeyLog($logdata);	
			
			$gamelist = $this->data_redis->keys("hu:{$playerID}:*");
			foreach($gamelist as $v){
				if($v=="hu:{$playerID}:oinfo"){
					$roomid = $this->data_redis->hget($v,'roomid');
					$roomdata = $this->cache_redis->hMget("hv:{$roomid}",array('ip','webport'));
					$url = "http://".$roomdata['ip'].":".$roomdata['webport']."/sync_user?userid=".$playerID;
					if(!$this->http->get($url)){
				    	$this->returnError(599, "通知异常！");
				    }
				}
			}
			$reqData = array('coin'=>$symoney,'money'=>$num,'room_card'=>$user['room_card']);
		}
	    //返回剩下的钻石
		$this->returnData($reqData);
		
	}

	public function after() {
		$this->deinitDataRedis();
		$this->deinitMysql();
	}
}

?>
